#include "common.h"
#include "base.h"
#include "dataloader.h"
#include <iostream>
#include <fstream>

Dataloader::Dataloader(std::string datadir) {
    std::string demand_csv = datadir + "/demand.csv";
    infile.open(demand_csv);
    std::string line;
    std::getline(infile, line);
    std::vector<std::string> sp = split(line, ",");
    for (int i = 1; i < int(sp.size()); i++) {
        cid_list.push_back(sp[i]);
    }
#ifdef DEBUG_DATALOADER
    for (int i = 0; i < int(cid_list.size()); i++) {
        std::cout << cid_list[i] << " ";
    }
    std::cout << std::endl;
#endif
}

bool Dataloader::load(demands &demands_in) {
    if (infile.eof()) {
        return false;
    } else {
        std::string line;
        std::getline(infile, line);
        std::vector<std::string> sp = split(line, ",");
        if (int(sp.size()) > 1) {
            demands_in.timestamp = sp[0];
            for (int i = 1; i < int(sp.size()); i++) {
                demands_in.demand_list.push_back(demand(cid_list[i - 1], std::stoi(sp[i])));
            }
#ifdef DEBUG_DATALOADER
            std::cout << demands_in.timestamp << ": ";
            for (int i = 0; i < int(demands_in.demand_list.size()); i++) {
                std::cout << demands_in.demand_list[i].cid << "-" << demands_in.demand_list[i].bw << " ";
            }
            std::cout << std::endl;
#endif
            return true;
        } else {
            return false;
        }
    }
}

Dataloader::~Dataloader() {
    infile.close();
}
